package com.ciner.main;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ciner.R;
import com.ciner.database.CinerDbAdapter;
import com.ciner.utility.Movie;
import com.ciner.utility.MovieListAdapter;

public class MovieListActivity extends Activity implements OnClickListener,
		OnItemClickListener {

	private final ArrayList<Movie> movie_data = new ArrayList<Movie>();
	private final ArrayList<Integer> movie_ids = new ArrayList<Integer>();
	private ListView movieList;

	static class ViewHolder {
		ImageView cover;
		TextView title;
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		movieList = (ListView) findViewById(R.id.movies_list);

		Button addButton = (Button) findViewById(R.id.add_button);
		addButton.setOnClickListener(this);

	}

	@Override
	public void onResume() {
		super.onResume();

		CinerDbAdapter dbAdapter = new CinerDbAdapter(MovieListActivity.this);
		dbAdapter.open();

		Cursor movieCursor = dbAdapter.fetchAllMovies();
		movieCursor.moveToFirst();

		while (!movieCursor.isAfterLast()) {
			movie_ids.add(movieCursor.getInt(0));
			movie_data.add(new Movie(movieCursor.getString(1), movieCursor
					.getInt(2), movieCursor.getInt(3), movieCursor.getInt(4),
					movieCursor.getString(5), movieCursor.getString(6),
					movieCursor.getString(7)));

			movieCursor.moveToNext();
		}

		Movie[] movieArray = new Movie[movie_data.size()];

		movieList.setAdapter(new MovieListAdapter(MovieListActivity.this,
				R.layout.movie_item, movie_data.toArray(movieArray)));

		dbAdapter.close();

	}

	public void onClick(View parent) {
		if (parent.equals(findViewById(R.id.add_button))) {

			Intent newMovieIntent =
					new Intent(MovieListActivity.this, NewMovieActivity.class);

			startActivity(newMovieIntent);

		}

	}

	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub

	}

}